package com.maixy.dailydemo.algorithm.audition;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @ClassName RemoveDuplicate
 * @Description 链表去重
 * @Date 2021/3/2 10:09
 * @Author maixy
 */
public class RemoveDuplicate {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(1);
        list.add(3);
        list.add(4);
        list.add(9);
        list.add(9);
        list = removeDuplicate(list);
        System.out.println(list);
    }

    public static List<Integer> removeDuplicate(List<Integer> list) {
        HashSet<Integer> set = new HashSet<>();
        List<Integer> result = new ArrayList<>();
        for (Integer str : list) {
            if (set.add(str)) {
                result.add(str);
            }
        }
        return result;
    }
}
